//
//  TraditionPOSListVC.m
//  NewPro
//
//  Created by 何荧红 on 2019/3/20.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "TraditionPOSListVC.h"
#import "TraditionPOSListCell.h"

#import "POSStore.h"
#import "PostlistModel.h"

#import "POSAddView.h"
#import "BusinessActionView.h"

#import "MyPOSlistVC.h"//我的
#import "ViewControllerTwo.h"//已划拨
#import "BackPOSRecordVC.h"//回拨记录
#import "CashbackRecordVC.h"//调整返现记录

@interface TraditionPOSListVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) ViewFoot *foot;
@property (nonatomic, strong) POSAddView *chooseView;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, copy) NSString *Gimage;

@property (nonatomic, strong) NSMutableDictionary *NumDic;
@property (nonatomic, assign) BOOL addViewPush;
@property (nonatomic, assign) NSInteger minPosFlow;
@end

@implementation TraditionPOSListVC

- (NSMutableDictionary *)NumDic{
    if (!_NumDic) {
        _NumDic = [[NSMutableDictionary alloc]init];
    }
    return _NumDic;
}
- (void)setAddViewPush:(BOOL)addViewPush{
    _addViewPush = addViewPush;
    if (_addViewPush) {
        self.chooseView.hidden = NO;
        self.chooseView.frame = CGRectMake(SCREEN_WIDTH - 140, 6, 135, 95);
    }else{
        self.chooseView.hidden = YES;
        self.chooseView.frame = CGRectMake(SCREEN_WIDTH - 140, 0, 135, 95);
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCSS];
    [self getPOSlist];
    [self getWithGimage];
}

- (void)onRightBtn{
    
    self.addViewPush = !self.addViewPush;
}

- (void)setNavBar{
    
    [self setNavBarWithTitle:@"传统POS终端查询"];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //修改按钮向左偏移10 point
    [rightBtn setFrame:CGRectMake(10.0, 0.0, 44.0, 44.0)];
    [rightBtn addTarget:self action:@selector(onRightBtn) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitle:@"+" forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:25];
    //修改方法
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 44.0, 44.0)];
    [view addSubview:rightBtn];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    
    self.navigationItem.rightBarButtonItem = rightItem;
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}

- (void)initCSS{
    
    WS(weakself);
    self.addViewPush = NO;
    
    [self setNavBar];
    
    self.foot = [[ViewFoot alloc]initWithPOSFoot];
    [self.view addSubview:_foot];
    
    [self.foot mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(75);
        make.left.right.bottom.mas_equalTo(self.view);
    }];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerClass:[TraditionPOSListCell class] forCellReuseIdentifier:@"TraditionPOSListCellID"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getPOSlist];
    }];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.foot.mas_top);
    }];
    self.chooseView = [[POSAddView alloc]init];
    self.chooseView.hidden = YES;
    [self.chooseView setPOSAddBlock:^(NSInteger but) {
        weakself.addViewPush = !weakself.addViewPush;
        if (but == 0) {
            BusinessActionView * actview = [[BusinessActionView alloc]init];
            actview.subimageurl = weakself.Gimage;
            [weakself.view addSubview:actview];
        }
        if (but == 1) {
            
        }
    }];
    [self.view addSubview:self.chooseView];
}

- (void)getWithGimage{
    [[[POSStore alloc]init] GetDivisionURLWithID:[UserDefaultsManager sharedInstance].user_id Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            self.Gimage = res[@"data"][@"transfer"];
        }else{
            [self showMBPError:res[@"msg"]];
        }
    } Failure:^(NSError * _Nonnull error) {
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (void)getPOSlist{
    [[[POSStore alloc]init] GetPOSCountWithID:[UserDefaultsManager sharedInstance].user_id Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            NSInteger mycount = [res[@"data"][@"posCount"] integerValue] + [res[@"data"][@"flowCount"] integerValue];
            [self.NumDic setObject:[NSString stringWithFormat:@"%@",res[@"data"][@"posCount"]] forKey:@"MyPosCount"];
            [self.NumDic setObject:[NSString stringWithFormat:@"%@",res[@"data"][@"flowCount"]] forKey:@"HePosCount"];
            self.foot.FristPosnumber = [NSString stringWithFormat:@"%ld",mycount];
            self.foot.SecondPosnumber = [NSString stringWithFormat:@"%@",res[@"data"][@"openedCount"]];
            self.foot.ThridPosnumber = [NSString stringWithFormat:@"%@",res[@"data"][@"activeCount"]];
        }else{
            [self showMBPError:res[@"msg"]];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } Failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self showMBPError:[HttpTool handleError:error]];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *foot = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    foot.backgroundColor = [UIColor clearColor];
    return foot;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TraditionPOSListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TraditionPOSListCellID"];
    if (indexPath.section == 0) {
        cell.titleStr = @"我的终端列表";
        if (self.NumDic.count > 0) {
            cell.NumStr = [NSString stringWithFormat:@"%@台",self.NumDic[@"MyPosCount"]];
        }
    }else if (indexPath.section == 1){
        cell.titleStr = @"已划拨终端列表";
        if (self.NumDic.count > 0) {
            cell.NumStr = [NSString stringWithFormat:@"%@台",self.NumDic[@"HePosCount"]];
        }
    }else if (indexPath.section == 2){
        cell.titleStr = @"自备机终端列表";
        if (self.NumDic.count > 0) {
            cell.NumStr = @"0台";
        }
    }else if (indexPath.section == 3){
        cell.titleStr = @"回拨记录";
        cell.NumStr = @"";
    }else if (indexPath.section == 4){
        cell.titleStr = @"调整返现记录";
        cell.NumStr = @"";
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        MyPOSlistVC *VC = [[MyPOSlistVC alloc]init];
        [[AppDelegate sharedAppDelegate] pushViewController:VC];
    }else if (indexPath.section == 1){
        [[AppDelegate sharedAppDelegate] pushViewController:[[ViewControllerTwo alloc]init]];
    }else if (indexPath.section == 2){
        
    }else if (indexPath.section == 3){
        [[AppDelegate sharedAppDelegate] pushViewController:[[BackPOSRecordVC alloc] init]];
    }else if (indexPath.section == 4){
        [[AppDelegate sharedAppDelegate] pushViewController:[[CashbackRecordVC alloc] init]];
    }
}


@end
